/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddMemberPanel.java
 *
 * Created on 2011-11-24, 15:07:47
 */
package mscs.client.member.view;


import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import mscs.client.view.MscsView;
import mscs.client.member.logic.AddMemberUtil;
import mscs.client.member.logic.IAddMemberUtil;
import mscs.data.pojo.Member;

/**
 *
 * @author Administrator
 */
public class AddMemberPanel extends javax.swing.JPanel {

    /** Creates new form AddMemberPanel */
    public AddMemberPanel(MscsView parent) {
        this.mscsView = parent;
        initComponents();
        setEnvironment();
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        delButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(420, 300));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mscs.client.view.MscsApp.class).getContext().getResourceMap(AddMemberPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(tableModel);
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            
        });
       
        jScrollPane1.setViewportView(jTable1);

        addButton.setText(resourceMap.getString("addButton.text")); // NOI18N
        addButton.setName("addButton"); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        delButton.setText(resourceMap.getString("delButton.text")); // NOI18N
        delButton.setName("delButton"); // NOI18N
        delButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delButtonActionPerformed(evt);
            }
        });

        editButton.setText(resourceMap.getString("editButton.text")); // NOI18N
        editButton.setName("editButton"); // NOI18N
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addButton)
                        .addGap(18, 18, 18)
                        .addComponent(delButton)
                        .addGap(18, 18, 18)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addButton)
                    .addComponent(delButton)
                    .addComponent(jButton4)
                    .addComponent(editButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
    	AddMemberDialog dialog = new AddMemberDialog(this.mscsView.getFrame(), true);
    	Member member = dialog.showDialog(addMemberUtil.generateID());
    	if(member != null){
    		addMemberUtil.addToNewList(member);
    		refreshTable();
    	}
     
    }//GEN-LAST:event_addButtonActionPerformed

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
    if(this.jTable1.getSelectedRowCount()!= 0){
        this.delButton.setEnabled(true);
        this.editButton.setEnabled(true);
    }
}//GEN-LAST:event_jTable1MouseClicked

private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
    this.addMemberUtil.removeFromNewList(this.jTable1.getSelectedRow());
    refreshTable();
}//GEN-LAST:event_delButtonActionPerformed

private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
    AddMemberDialog dialog = new AddMemberDialog(this.mscsView.getFrame(), true, 
            addMemberUtil.getByIndex(this.jTable1.getSelectedRow()));
    Member member = dialog.showDialog(addMemberUtil.getByIndex(this.jTable1.getSelectedRow()).getId());
    if(member != null){
        addMemberUtil.setByIndex(this.jTable1.getSelectedRow(), member);
        refreshTable();
    }
}//GEN-LAST:event_editButtonActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    if(this.addMemberUtil.commitNewList() == true){
        JOptionPane.showMessageDialog(this, "提交成功");
    }else{
        JOptionPane.showMessageDialog(this, "请勿提交空表");
    }
   refreshTable();
}//GEN-LAST:event_jButton4ActionPerformed
    
  public void refreshTable(){
	this.tableModel.setDataVector(TableModelFactory.MemberListDataVector(this.addMemberUtil.getNewList()), 
                                                                        TableModelFactory.getColumnNames());
                  this.delButton.setEnabled(false);
                  this.editButton.setEnabled(false);
  }
	
   public void setEnvironment(){
	delButton.setEnabled(false);
	editButton.setEnabled(false);
   }
	
	
DefaultTableModel tableModel = mscs.client.member.view.TableModelFactory.getNullMemberModel();
MscsView mscsView ;
IAddMemberUtil addMemberUtil = new AddMemberUtil();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton delButton;
    private javax.swing.JButton editButton;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
